/*
 * @lc app=leetcode.cn id=114 lang=cpp
 *
 * [114] 二叉树展开为链表
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
    vector<TreeNode*> vec;
    void dfs(TreeNode* root)
    {
        if(!root) return;
        vec.push_back(root);
        dfs(root->left);
        dfs(root->right);
    }
    void flatten(TreeNode* root) {
        if(!root) return;
        dfs(root);
        for(int i = 1; i < vec.size(); i++)
        {
            vec[i-1]->left = nullptr;
            vec[i-1]->right = vec[i];
        }
        vec[vec.size() - 1]->left = nullptr;
        vec[vec.size() - 1]->right = nullptr;
        return;
    }
};
// @lc code=end

